#!/bin/sh

main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html

for key in ap_id netbar_wacode apcorp longitude latitude location_type operator_net
do

eval ${key}="$(grep "^${key} " /etc/sbnsysconfig.conf | awk '{print $2}')"

done

for key in NetsiteCode GwInterface
do

eval ${key}="$(cat /etc/sbnpcap.conf| sed 's/\"//g' | grep ${key} |awk -F: '{print $2}')"

done



cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-2-5">
				<h3>双栢年设置</h3>
EOF

cat <<EOF
<script>
\$(function(){
  \$('#sbn_setting').on('submit', function(e){
    e.preventDefault();
    var data = "app=sbn&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'ajax-fluid');
  });

  \$('#sbnpcap_setting').on('submit',function(e){
    e.preventDefault();
    var data = "app=sbn&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'ajax-fluid');
  });
});
</script>
EOF
cat <<EOF
			  <h3>设置</h3>
			  <form id="sbn_setting">
				<table class="pure-table pure-table-bordered">

				<tr>
				<td>
				和泰联接口apid
				</td>
				<td>
				<input type="text" name="ap_id" value="$ap_id">
				</td>
				</tr>

				<tr>
				<td>
				安装地点编码
				</td>
				<td>
				<input type="text" name="netbar_wacode" value="$netbar_wacode">
				</td>
				</tr>

				<tr>
				<td>
				安全机构代码
				</td>
				<td>
				<input type="text" name="apcorp" value="$apcorp">
				</td>
				</tr>

				<tr>
				<td>
				<font color="red">ap所在的经度</font>
				</td>
				<td>
				<input type="text" name="longitude" value="$longitude">
				</td>
				</tr>

				<tr>
				<td>
				<font color="red">ap所在的纬度</font>
				</td>
				<td>
				<input type="text" name="latitude" value="$latitude">
				</td>
				</tr>

				<tr>
				<td>
				安装地点类型
				</td>
				<td>
				<input type="text" name="location_type" value="$location_type">
				</td>
				</tr>

				<tr>
				<td>
				网络类型
				</td>
				<td>
				<input type="text" name="operator_net" value="$operator_net">
				</td>
				</tr>

				<tr>
				<td>
				操作
				</td>
				<td>
				<input type="hidden" name="action" value="post_save_sbn">
				<button type="submit" class="pure-button">保存</button>
				</td>
				</tr>
				</table>
			  </form>
EOF

cat <<EOF
<h3>审计抓包设置</h3>
<form id="sbnpcap_setting">
    <table class="pure-table pure-table-bordered">
    <tr>
        <td>
            场所服务编码
        </td>
        <td>
            <input type="text" name="NetsiteCode" value="$NetsiteCode">
        </td>
    </tr>
    <tr>
        <td>
            审计LAN口标识
        </td>
        <td>
            <input type="text" name="GwInterface" value="$GwInterface">
        </td>
    </tr>
    <tr>
        <td>
            操作
        </td>
        <td>
            <input type="hidden" name="action" value="post_save_sbnpcap">
            <button type="submit" class="pure-button">保存</button>
        </td>
    </tr>
    </table>
</form>
EOF

cat <<EOF
			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-3-5">
				<h3>配置信息</h3>
<pre style="font-size:small">
<p>wan mac地址: $(ifconfig $(uci get network.wan.ifname) | grep -Eo '..:..:..:..:..:..' | tr -d ':')</p>
<p>br-lan mac地址:</p>
<p> $(ifconfig $(uci get network.br-lan.ifname) | grep -Eo '..:..:..:..:..:..' | tr -d ':')</p>
</pre>
			</div>
		</div>
EOF
if
[ $(echo "$lans" | wc -l) -eq 1 ]
then
footer_html keep_bottom
else
footer_html
fi
}
post_save_sbn()
{
for key in ap_id netbar_wacode apcorp longitude latitude location_type operator_net
do
	var=$(eval echo '$FORM_'${key})
	if
	[ -z "$var" ]
	then
		echo "缺少参数,设置失败!!" | main.sbin output_json 1
		exit
	fi
done

for key in ap_id netbar_wacode apcorp longitude latitude location_type operator_net
do
	var=$(eval echo '$FORM_'${key} | tr -d ' ')
	sed -i "s#^${key} .*#${key} ${var}#g" /etc/sbnsysconfig.conf
done

echo "设置成功!!" | main.sbin output_json 0
exit
}

post_save_sbnpcap()
{
for key in NetsiteCode GwInterface
do
        var=$(eval echo '$FORM_'${key})
        if
        [ -z "$var" ]
        then
                echo "缺少参数,设置失败!!" | main.sbin output_json 1
                exit
        fi
done

for key in NetsiteCode GwInterface
do
        var=$(eval echo '$FORM_'${key} | tr -d ' ')
        sed -i "s#\"${key}.*#\"${key}\":\"${var}\"#g" /etc/sbnpcap.conf
done

echo "设置成功!!" | main.sbin output_json 0
exit
}

lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi

